﻿using Pb;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class NewNextGame : Game
{

    public List<AudioClip> TextaudioClipListMan;
    public List<AudioClip> TextaudioClipListWomen;
    public UILabel roomInfoLable;

    //Prafab path
   
    private string ChiHuPengGangPath = "GameMahjong/NextGameP/ChiHuPengGang";
    private string CardsPath = "GameMahjong/NextGameP/MahjongCards";
    
    private string SetPath = GameDNPrafabPath.Set;
    public GameObject BackBtn;
    public GameObject SetBtn;
    public GameObject ReadyBtn;
    public GameObject ChatBtn;
    

    public GameObject roomInfo;
    public List<GameObject> UserList;
    public GameObject Cards;
    private int LastNextSid; //存储上次谁出的牌  (可以删除)
    private CardModel cardModel;
    
    public SpecialCardModel specialCardModel;
    //剩余牌数
    EginUser user = EginUser.Instance;
    EginMahjong mahjong = EginMahjong.Instance;
    //特殊牌提示按钮
    public List<GameObject> specialCardBtn;

    private  GameOverShow gameOverShow;
    //每轮牌第一次加载
    private bool FirstLoad=true;
    //胡的牌

    private string[] mahjongText = {
        "快点吧，我等的花都谢了",
        "你的牌打得太好了！",
        "大家好很高兴见到各位",
        "哈哈，我胡了",
        "好歹让我吃一个嘛",
        "打一个来碰噻",
        "和你合作真是太愉快了啊",
        "来啊互相伤害啊",
        "还让不让我摸牌了",
        "你这样以后没朋友"
    };
    private void Awake()
    {
        //屏幕横屏
        Screen.orientation = ScreenOrientation.LandscapeLeft;

        
    }
    private void Start()
    {
        //StartCoroutine(DoDiceAnimation());
        Screen.orientation = ScreenOrientation.AutoRotation;
        Screen.autorotateToLandscapeLeft = true;
        Screen.autorotateToLandscapeRight = true;
        Screen.autorotateToPortrait = false;
        Screen.autorotateToPortraitUpsideDown = false;
        //加载房间状态
        roomInfoShow();

        LoadGame();
    }
   
    #region 加载游戏状态
    void LoadGame()
    {

        EginUser MainUser = EginUser.Instance;
        if (mahjong.roomStatus == ROOM_STATUS.Gaming)
        {
            
            LoadUser();
            #region 重连后加载玩家加载card user 和吃碰杠胡状态

            //Cards.SetActive(true);
            LoadModel(CardsPath);
            cardModel = GameObject.Find("MahjongCards(Clone)").GetComponent<CardModel>();

            //
            if (mahjong.mahjongSpecialCardItem.Count != 0)
            {
                if (mahjong.mahjongSpecialCardItem[0].TargetCard == 0)
                {
                    mahjong.tempMahjongActionType = MAHJONG_ACTION_TYPE.OnlyOutCard;
                }
                else
                {
                    GameObject go = LoadModelReturnObj(ChiHuPengGangPath);
                    go.GetComponent<SpecialCardModel>().LoadSpecialCard();
                }
            }
            else
            {
                mahjong.tempMahjongActionType = MAHJONG_ACTION_TYPE.Normal;

            }
            if (mahjong.isSpecialPhases2)
            {
                mahjong.tempMahjongActionType = MAHJONG_ACTION_TYPE.OnlyOutCard;
            }

            #endregion

        }
        else
        {
            #region 创建房间加入房间后显示房间信息
            if (mahjong.uid == mahjong.CteatrUid)
            {
                mahjong.OtherCanClick = true;
                if (mahjong.seatStatus != SEAT_STATUS.Ready)
                {
                    ReadyBtn.SetActive(true);
                }
                if (mahjong.FirstLoadUser)
                {
                    UserList[0].SetActive(true);
                    //UserList[0].GetComponent<UserStatuModel>().SeatID = (uint)MainUser.seatID;
                    UserList[0].GetComponent<UserStatuModel>().LoadInfo(1);

                }
            }
            else
            {
                mahjong.OtherCanClick = false;
                if (mahjong.seatStatus != SEAT_STATUS.Ready)
                {
                    ReadyBtn.SetActive(true);
                }
            }
            #endregion

            LoadUser();
        }

        //显示庄
        if (mahjong.bankerSid != 0)
        {
            int addIndex = returnOtherSeatIndex(mahjong.bankerSid) - 1;
            UserList[addIndex].SetActive(true);
            UserList[addIndex].GetComponent<UserStatuModel>().Zhuang.GetComponent<UISprite>().enabled = true;
        }


        base.StartGameSocket();
        SocketManager.Instance.socketListener = this;
        UIEventListener.Get(BackBtn).onClick = ButtonClick;
        UIEventListener.Get(SetBtn).onClick = ButtonClick;
        UIEventListener.Get(ReadyBtn).onClick = ButtonClick;
        UIEventListener.Get(ChatBtn).onClick = ButtonClick;
        
        //UIEventListener.Get(RewordBtn).onClick = ButtonClick;
        //加载玩家
        //重连第一个存储上个出牌的seatid
        LastNextSid = mahjong.nextSid;
        mahjong.LastNextSid = mahjong.nextSid;

        //加载OverClose 申请结束游戏
        //if (MainUser.requestStatusList.Count > 0)
        //{

        //}
        if (mahjong.tingCards != null) {
            //重连听牌
            if (mahjong.tingCards.Count > 0 && mahjong.tingCards != null)
            {

                cardModel.tingCard.SetActive(true);
                cardModel.tingBgList[0].SetActive(true);
                cardModel.tingBgList[0].GetComponent<UISprite>().width = 43 + 26 * (mahjong.tingCards.Count - 1);
                for (int i = 0; i < mahjong.tingCards.Count; i++)
                {
                    cardModel.tingCardList[i].gameObject.SetActive(true);
                    cardModel.tingCardList[i].spriteName = mahjong.tingCards[i].ToString();
                }
                mahjong.tingCards.Clear();
            }
        }
       
        
    }
    #endregion 加载

    void ButtonClick(GameObject button)
    {
        if (button.name == "BackBtn")
        {
            BackBtnEvent();
        }
        else if (button.name == "SetBtn")
        {
            // DNSet
            GameObject Set = NGUITools.AddChild(GameObject.Find("UI Root"), (GameObject)(Resources.Load(GameDNPrafabPath.Set)));
        }
        else if (button.name == "ChatBtn")
        {

            GameObject DDZChat = NGUITools.AddChild(GameObject.Find("UI Root"), (GameObject)(Resources.Load("GameMahjong/MahjongChat")));
        }
        
        else if (button.name == "ReadyBtn")
        {
            RoomReadyRequest RR = new Pb.RoomReadyRequest {  RoomId =mahjong.roomID };
            SocketManager socketManager = SocketManager.Instance;
            socketManager.SendProtobuf1(RR, MsgId.RoomReadyRequest);
            ReadyBtn.SetActive(false);
        }
     
    }
    public override void SocketReceiveMessage(byte[] data)
    {


        base.SocketReceiveMessage(data);
        byte[] dataLength = data.Skip(0).Take(2).ToArray();
        byte[] dataId = data.Skip(2).Take(2).ToArray();
        byte[] dataConcent = data.Skip(data.Length - (data.Length - 4)).ToArray();
        int TempMsgID = ProtoDataToMsg.Byte2ushort(dataId);

        #region  房间消息处理
        //处理解散房间返回消息
        if (TempMsgID == MsgId.RoomDismissResponse)
        {
            Pb.RoomDismissResponse newpb = Pb.RoomDismissResponse.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);
            Debug.LogError("---"+newpb.Msg);
            if (newpb.Status == STATUS.Success)
            {

            }

        }
        //处理解散房间
        else if (TempMsgID == MsgId.RoomDismissBroadcast)
        {
            Pb.RoomDismissBroadcast newpb = Pb.RoomDismissBroadcast.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);
            if (newpb.ReasonId == RoomDismissReason.CreaterDismiss)
            {
                //cardModel.gameObject.GetComponent<GameOverShow>().NextBtn.SetActive(false);
                Application.LoadLevel("Hall");
                ClearRecord();
            }
            else if (newpb.ReasonId == RoomDismissReason.SelfLeave)
            {
                //cardModel.gameObject.GetComponent<GameOverShow>().NextBtn.SetActive(false);
                Application.LoadLevel("Hall");
                ClearRecord();
            }
            else if (newpb.ReasonId == RoomDismissReason.SomeoneLeave)
            {
                //cardModel.gameObject.GetComponent<GameOverShow>().NextBtn.SetActive(false);
                Application.LoadLevel("Hall");
                ClearRecord();
            }
            else if (newpb.ReasonId == RoomDismissReason.SelfBroke)  //游戏币不足，被踢出游戏
            {

                StartCoroutine(GoHall(3.0F));
                user.logMsg = "金币不足，请充值";
                LoadModel("LogMsg");
                ClearRecord();
            }
            else if (newpb.ReasonId == RoomDismissReason.SomeoneBroke)  //房间中有玩家金币不足，被踢出房间，人数不足了，解散
            {

                StartCoroutine(GoHall(3.0F));
                user.logMsg = "有玩家金币不足，房间解散";
                LoadModel("LogMsg");
                ClearRecord();
            }
            else if (newpb.ReasonId == RoomDismissReason.SomeoneRobot)  //有玩家离线、或者机器人代打了
            {
                StartCoroutine(GoHallBot(3.0F));
                cardModel.gameObject.GetComponent<GameOverShow>().NextBtn.SetActive(false);
                cardModel.gameObject.GetComponent<GameOverShow>().ExitRoomBtn.transform.localPosition = new Vector3(19, -290, 0);
                cardModel.gameObject.GetComponent<GameOverShow>().botDismiss = true;

            }



        }
        //处理本人离开房间消息
        else if (TempMsgID == MsgId.RoomLeaveResponse)
        {
            #region    处理本人离开房间消息
            Pb.RoomLeaveResponse newpb = Pb.RoomLeaveResponse.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);
            if (newpb.Status == STATUS.Success)
            {
                Application.LoadLevel("Hall");
                ClearRecord();
            }
            else
            {
                user.logMsg = newpb.Msg;
                LoadModel("LogMsg");
                Debug.Log(newpb.Msg);
            }
            #endregion
        }
        //处理本人以外玩家加入离开消息
        else if (TempMsgID == MsgId.RoomUserInfoBroadcast)
        {

            #region    处理玩家加入房间离开房间消息
            Pb.RoomUserInfoBroadcast newpb = Pb.RoomUserInfoBroadcast.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);

            //加入房间
            if (newpb.UserInfo.SeatStatus == SEAT_STATUS.Sit)
            {
                int tempSeatID = newpb.UserInfo.SeatId;
                if (!mahjong.Otheruid.ContainsKey(tempSeatID))
                {
                    mahjong.Otheruid.Add(tempSeatID, newpb.UserInfo.Uid);
                    mahjong.OtherSeatId.Add(tempSeatID, newpb.UserInfo.SeatId);
                    mahjong.OtherSeatStatus.Add(tempSeatID, newpb.UserInfo.SeatStatus);
                    mahjong.OtherSex.Add(tempSeatID, newpb.UserInfo.Sex);
                    mahjong.OtherIP.Add(tempSeatID, newpb.UserInfo.Ip);
                    mahjong.OtherCoinCount.Add(tempSeatID, newpb.UserInfo.BagCoinCount);
                    mahjong.OtherScore.Add(tempSeatID, newpb.UserInfo.Score);
                    mahjong.OtherNickName.Add(tempSeatID, newpb.UserInfo.Nickname);
                    mahjong.OtherAvatarid.Add(tempSeatID, newpb.UserInfo.AvatarId);
                }
                user.gameUserInfoList.Add(newpb.UserInfo);
                Debug.Log("返回其他玩家相对于本人的位置索引" + returnOtherSeatIndex((int)tempSeatID) + "seatid:" + tempSeatID);
                int addIndex = returnOtherSeatIndex((int)tempSeatID) - 1;
                UserList[addIndex].SetActive(true);
                UserList[addIndex].GetComponent<UserStatuModel>().LoadInfo(tempSeatID);
                UserList[addIndex].GetComponent<UserStatuModel>().sitStatu();
                //UserList[addIndex].GetComponent<UserStatuModel>().SeatID = tempSeatID;
            }
            else if (newpb.UserInfo.SeatStatus == SEAT_STATUS.Leave)
            {
                int tempSeatID = newpb.UserInfo.SeatId;
                mahjong.Otheruid.Remove(tempSeatID);
                mahjong.OtherSeatId.Remove(tempSeatID);
                mahjong.OtherSeatStatus.Remove(tempSeatID);
                mahjong.OtherSex.Remove(tempSeatID);
                mahjong.OtherIP.Remove(tempSeatID);
                mahjong.OtherCoinCount.Remove(tempSeatID);
                mahjong.OtherScore.Remove(tempSeatID);
                mahjong.OtherNickName.Remove(tempSeatID);
                mahjong.OtherAvatarid.Remove(tempSeatID);

                //离开房间
                for (int i = 0; i < user.gameUserInfoList.Count; i++)
                {
                    if (newpb.UserInfo.SeatId == user.gameUserInfoList[i].SeatId)
                    {
                        user.gameUserInfoList.RemoveAt(i);
                        UserList[returnOtherSeatIndex((int)tempSeatID) - 1].SetActive(false);
                        UserList[returnOtherSeatIndex((int)tempSeatID) - 1].GetComponent<UserStatuModel>().wxAvatar.gameObject.SetActive(false) ;
                        break;
                    }
                }
               
            }
            else if (newpb.UserInfo.SeatStatus == SEAT_STATUS.Ready)
            {
                int tempSeatID = newpb.UserInfo.SeatId;
                int addIndex = returnOtherSeatIndex((int)tempSeatID) - 1;

                if (UserList[addIndex].activeSelf == false && newpb.UserInfo.AvatarId != 0)
                {
                    Debug.Log(111);
                    //int tempSeatID = newpb.UserInfo.SeatId;
                    if (!mahjong.Otheruid.ContainsKey(tempSeatID))
                    {
                        mahjong.Otheruid.Add(tempSeatID, newpb.UserInfo.Uid);
                        mahjong.OtherSeatId.Add(tempSeatID, newpb.UserInfo.SeatId);
                        mahjong.OtherSeatStatus.Add(tempSeatID, newpb.UserInfo.SeatStatus);
                        mahjong.OtherSex.Add(tempSeatID, newpb.UserInfo.Sex);
                        mahjong.OtherIP.Add(tempSeatID, newpb.UserInfo.Ip);
                        mahjong.OtherCoinCount.Add(tempSeatID, newpb.UserInfo.BagCoinCount);
                        mahjong.OtherScore.Add(tempSeatID, newpb.UserInfo.Score);
                        mahjong.OtherNickName.Add(tempSeatID, newpb.UserInfo.Nickname);
                        mahjong.OtherAvatarid.Add(tempSeatID, newpb.UserInfo.AvatarId);
                    }
                    user.gameUserInfoList.Add(newpb.UserInfo);
                    Debug.Log("返回其他玩家相对于本人的位置索引" + returnOtherSeatIndex((int)tempSeatID) + "seatid:" + tempSeatID);
                    //int addIndex = returnOtherSeatIndex((int)tempSeatID) - 1;
                    UserList[addIndex].SetActive(true);
                    UserList[addIndex].GetComponent<UserStatuModel>().LoadInfo(tempSeatID);
                    //UserList[addIndex].GetComponent<UserStatuModel>().sitStatu();
                }

                UserList[addIndex].GetComponent<UserStatuModel>().readyStatu();

            }
            else if (newpb.UserInfo.SeatStatus == SEAT_STATUS.Offline)
            {
                int tempSeatID = newpb.UserInfo.SeatId;
                int addIndex = returnOtherSeatIndex((int)tempSeatID) - 1;

                UserList[addIndex].GetComponent<UserStatuModel>().LeaveStatu();
            }
            #endregion

        }


        #region majiang处理更改seatid
        //处理更改seatid
        else if (TempMsgID == MsgId.NewSeatIdNotification)
        {
            Pb.NewSeatIdNotification newpb = Pb.NewSeatIdNotification.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);
            for (int j = 0; j < user.gameUserInfoList.Count; j++)
            {
                if (mahjong.bankerSid == user.gameUserInfoList[j].SeatId)
                {
                    for (int i = 0; i < newpb.OldSidList.Count; i++)
                    {
                        if (user.gameUserInfoList[j].Uid == newpb.OldSidList[i])
                        {
                            mahjong.bankerSid = newpb.NewSidList[i];
                        }
                    }
                }

            }
            for (int i = 0; i < newpb.OldSidList.Count; i++)
            {
                if (newpb.OldSidList[i] == mahjong.uid)
                {
                    mahjong.seatID = newpb.NewSidList[i];
                    mahjong.LoadIndex(newpb.NewSidList[i]);
                    break;
                }

            }
           
            for (int i = 0; i < newpb.OldSidList.Count; i++)
            {
                for (int j = 0; j < user.gameUserInfoList.Count; j++)
                {
                    if (user.gameUserInfoList[j].Uid == newpb.OldSidList[i])
                    {
                        user.gameUserInfoList[j].SeatId = newpb.NewSidList[i];
                        break;
                    }
                }
            }
            LoadUser();                     
        }

        #endregion majiang处理更改seatid


        //准备消息
        else if (TempMsgID == MsgId.RoomReadyResponse)
        {
            Pb.RoomReadyResponse newpb = Pb.RoomReadyResponse.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);

            if (newpb.Status == STATUS.Success)
            {
                ReadyBtn.SetActive(false);
            }
        }

        else if (TempMsgID == MsgId.MahjongRoomBeginNotification)
        {
            #region    四人准备后发牌消息MahjongRoomBeginNotification
            Pb.MahjongRoomBeginNotification newpb = Pb.MahjongRoomBeginNotification.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);

            
            //GameObject overShow = GameObject.Find("UI Root/MahjongCards(Clone)/GameOverInfo");
            //if(overShow != null){
            //    overShow.SetActive(false);
            //}

            mahjong.currentRound = newpb.CurrentRound;
            roomInfoShow();
            mahjong.bankerSid = newpb.BankerSid;
            mahjong.nextSid = mahjong.bankerSid;
            LastNextSid = mahjong.bankerSid;
            mahjong.LastNextSid = mahjong.bankerSid;
            mahjong.LastSeatID = mahjong.bankerSid;
            mahjong.roomStatus = newpb.RoomStatus;
            mahjong.staySid = newpb.StaySid;
            mahjong.stayCount = newpb.StayCount;
            mahjong.handCard = newpb.HandCard;
            List<int> diceList = new List<int>();
            diceList = newpb.DiceList.ToList();
            mahjong.diceOne = diceList[0];
            mahjong.diceTwo = diceList[1];
            //List<int> cardList = new List<int>();
            mahjong.CardList = newpb.CardList.ToList();
            //剩余牌
            mahjong.remainCount = 60;



            if (mahjong.LastBankSid != 0)
            {
                int tempindex = returnOtherSeatIndex(mahjong.LastBankSid) - 1;
                UserList[tempindex].GetComponent<UserStatuModel>().Zhuang.GetComponent<UISprite>().enabled = false;
            }
            mahjong.LastBankSid = newpb.BankerSid;
            int addIndex = returnOtherSeatIndex(mahjong.bankerSid) - 1;
            UserList[addIndex].SetActive(true);
            UserList[addIndex].GetComponent<UserStatuModel>().Zhuang.GetComponent<UISprite>().enabled = true;
            //隐藏准备图片
            for (int i = 0; i < UserList.Count; i++)
            {
                UserList[i].GetComponent<UserStatuModel>().OKSprit.SetActive(false);
            }
            List<int> readyCards = newpb.ReadyCards.ToList();
            //筛子动画
            StartCoroutine(DoDiceAnimation(readyCards));

            //gameOverShow = GameObject.Find("UI Root/MahjongCards(Clone)").GetComponent<GameOverShow>();
           
            #endregion    四人准备后发牌消息MahjongRoomBeginNotification    
        }
        #endregion

        #region 游戏消息处理

        if (TempMsgID == MsgId.MahjongPlayResponse)
        {
            Pb.MahjongPlayResponse newpb = Pb.MahjongPlayResponse.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);
            if (newpb.Status == STATUS.Success) {
                //听牌
                if (newpb.ReadyCards.Count > 0)
                {
                    cardModel.tingCard.SetActive(true);
                    cardModel.tingBgList[0].SetActive(true);
                    Debug.Log("ReadyCards" + newpb.ReadyCards.Count);
                    for (int i = 0; i < cardModel.tingCardList.Count; i++)
                    {
                        cardModel.tingCardList[i].gameObject.SetActive(false);
                    }


                    cardModel.tingBgList[0].GetComponent<UISprite>().width = 43 + 26 * (newpb.ReadyCards.Count - 1);
                    for (int i = 0; i < newpb.ReadyCards.Count; i++)
                    {
                        cardModel.tingCardList[i].gameObject.SetActive(true);
                        cardModel.tingCardList[i].spriteName = newpb.ReadyCards[i].ToString();
                    }
                }
                else
                {
                    cardModel.tingCard.SetActive(false);
                    for (int i = 0; i < cardModel.tingCardList.Count; i++)
                    {

                        cardModel.tingCardList[i].gameObject.SetActive(false);
                    }
                }
            }
            
        }
        //服务器计算，有可以吃、碰的，就多等几秒后再发牌
        else if (TempMsgID == MsgId.MahjongPlayBroadcast)
        {
            Pb.MahjongPlayBroadcast newpb = Pb.MahjongPlayBroadcast.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);

            mahjong.tempSeatId = newpb.SeatId;
            mahjong.nextSid = newpb.NextSid;
            mahjong.handCard = newpb.HandCard;
            int tempIncard = newpb.InCard;
            Debug.Log("tempIncard:" + tempIncard);
            int tempOutcard = newpb.OutCard;
            if (tempOutcard != 0)
            {
                mahjong.LastSeatID = newpb.SeatId;
                Debug.Log("上局出牌人的seatid：" + mahjong.LastSeatID);
            }
            //显示托管头像
            if (newpb.IsRobot)
            {
                int tempIndex = returnOtherSeatIndex(newpb.SeatId) - 1;
                UserList[tempIndex].GetComponent<UserStatuModel>().Cover_TuoGuan.SetActive(true);
            }
            else {
                int tempIndex = returnOtherSeatIndex(newpb.SeatId) - 1;
                UserList[tempIndex].GetComponent<UserStatuModel>().Cover_TuoGuan.SetActive(false);
            }
            if (mahjong.tempSeatId == mahjong.seatID) {
                mahjong.havaSpecailCard = false;
            }
            #region 出牌处理
            FirstLoad = false;

            if (newpb.ActionType == MAHJONG_ACTION_TYPE.Normal)
            {
                if (tempOutcard != 0)
                {
                    RecordRemainCount();
                }
                #region MAHJONG_ACTION_TYPE.Normal
                mahjong.tempMahjongActionType = MAHJONG_ACTION_TYPE.Normal;
                if (newpb.HandCard == 0)
                {

                    if (mahjong.seatID == mahjong.tempSeatId)
                    {
                        //处理自己的出牌消息
                        cardModel.OwnOutCard(tempOutcard, tempIncard);
                        if (newpb.IsRobot)
                        {
                            cardModel.AutoOutCard.SetActive(true);
                        }
                    }
                    else
                    {
                        //处理他人的出牌消息
                        cardModel.OtherOutCard(tempOutcard, tempIncard, returnOtherSeatIndex(mahjong.tempSeatId));
                    }
                }
                else
                {

                    //处理摸牌消息
                    cardModel.OtherOutCard(tempOutcard, tempIncard, returnOtherSeatIndex(mahjong.tempSeatId));
                    //处理到手的card
                    cardModel.MoCard();
                }
                #endregion MAHJONG_ACTION_TYPE.Normal
            }
            else if (newpb.ActionType == MAHJONG_ACTION_TYPE.MjPass)
            {
                #region MAHJONG_ACTION_TYPE.Pass
                mahjong.tempMahjongActionType = MAHJONG_ACTION_TYPE.Normal; //{ "seatId": 1, "nextSid": 1, "handCard": 12 }
                //if (specialCardBtn.Count>0)
                //{
                //    for (int i=0;i< specialCardBtn.Count;i++) {
                //        Destroy(specialCardBtn[i]);
                //        specialCardBtn.RemoveAt(i);
                //    }      
                //}
                if (newpb.SeatId == mahjong.seatID) {
                    if (GameObject.Find("ChiHuPengGang(Clone)") != null) {
                        Destroy(GameObject.Find("ChiHuPengGang(Clone)"));
                    }
                    if (newpb.IsRobot)
                    {
                        cardModel.AutoOutCard.SetActive(true);
                    }
                    //整理牌
                    
                    for (int i = 0; i < cardModel. CardListObj.Count; i++)
                    {
                        cardModel. CardListObj[i].transform.localPosition = new Vector3(cardModel.CardListObj[i].transform.localPosition.x, -307.8f, 0);
                        cardModel. CardListObj[i].GetComponent<CardButton>().TempBool = false;
                    }
                }
                
                if (newpb.HandCard == 0)
                {
                    
                    
                }
                else
                {
                    cardModel.MoCard();
                }
                #endregion MAHJONG_ACTION_TYPE.Pass
            }
            else if (newpb.ActionType == MAHJONG_ACTION_TYPE.OnlyOutCard) //outcard 出牌消息
            {

                #region MAHJONG_ACTION_TYPE.OnlyOutCard  
                mahjong.tempMahjongActionType = MAHJONG_ACTION_TYPE.Normal;
                if (newpb.HandCard == 0)
                {

                    if (mahjong.seatID == mahjong.tempSeatId)
                    {
                        //处理自己的出牌消息
                        cardModel.OwnOutCard(tempOutcard, tempIncard);
                        if (newpb.IsRobot)
                        {
                            cardModel.AutoOutCard.SetActive(true);
                        }
                    }
                    else
                    {
                        //处理他人的出牌消息
                        cardModel.OtherOutCard(tempOutcard, tempIncard, returnOtherSeatIndex(mahjong.tempSeatId));
                    }
                }
                else
                {
                    //处理摸牌消息
                    cardModel.OtherOutCard(tempOutcard, tempIncard, returnOtherSeatIndex(mahjong.tempSeatId));
                    //处理到手的card
                    cardModel.MoCard();
                }
                #endregion MAHJONG_ACTION_TYPE.OnlyOutCard
            }
            else if (newpb.ActionType == MAHJONG_ACTION_TYPE.Peng)
            {
                #region MAHJONG_ACTION_TYPE.Peng  
                if (newpb.SeatId == mahjong.seatID)
                {
                   
                    //整理牌

                    for (int i = 0; i < cardModel.CardListObj.Count; i++)
                    {
                        cardModel.CardListObj[i].transform.localPosition = new Vector3(cardModel.CardListObj[i].transform.localPosition.x, -307.8f, 0);
                        cardModel.CardListObj[i].GetComponent<CardButton>().TempBool = false;
                    }
                }

                mahjong.tempMahjongActionType = MAHJONG_ACTION_TYPE.OnlyOutCard;

                if (mahjong.seatID == mahjong.tempSeatId)
                {
                    //处理自己的碰牌消息
                    cardModel.OwnChiCard(tempIncard, tempIncard, tempIncard);
                }
                else
                {
                    //处理他人的碰牌消息
                    cardModel.OtherChiCard(tempIncard, tempIncard, tempIncard, returnOtherSeatIndex(mahjong.tempSeatId));
                }
                #endregion MAHJONG_ACTION_TYPE.Peng
            }
            else if (newpb.ActionType == MAHJONG_ACTION_TYPE.Chi)
            {
                #region MAHJONG_ACTION_TYPE.Chi
                mahjong.tempMahjongActionType = MAHJONG_ACTION_TYPE.OnlyOutCard;
                //int siblingCard1 = newpb.SiblingCardList[0];
               // int siblingCard2 = newpb.SiblingCardList[1];

                if (mahjong.seatID == mahjong.tempSeatId)
                {
                    //处理自己的吃牌消息
                  //  cardModel.OwnChiCard(tempIncard, siblingCard1, siblingCard2);
                }
                else
                {
                    //处理他人的吃牌消息
                    //cardModel.OtherChiCard(tempIncard, siblingCard1, siblingCard2, returnOtherSeatIndex(user.tempSeatId));
                }
                #endregion MAHJONG_ACTION_TYPE.Chi
            }
            else if (newpb.ActionType == MAHJONG_ACTION_TYPE.AddGang)
            {
                RecordRemainCount();
                RecordRemainCount();

                #region MAHJONG_ACTION_TYPE.AddGang
                if (newpb.SeatId == mahjong.seatID)
                {

                    //整理牌

                    for (int i = 0; i < cardModel.CardListObj.Count; i++)
                    {
                        cardModel.CardListObj[i].transform.localPosition = new Vector3(cardModel.CardListObj[i].transform.localPosition.x, -307.8f, 0);
                        cardModel.CardListObj[i].GetComponent<CardButton>().TempBool = false;
                    }
                }
                mahjong.tempMahjongActionType = MAHJONG_ACTION_TYPE.OnlyOutCard;
                if (newpb.HandCard == 0)
                {
                    if (mahjong.seatID == mahjong.tempSeatId)
                    {
                        //处理自己的杠牌消息
                        cardModel.OwnAddGangCard(tempIncard);
                    }
                    else
                    {
                        //处理他人的杠牌消息
                        cardModel.OtherAddGangCard(tempIncard, returnOtherSeatIndex(mahjong.tempSeatId));
                    }
                }
                else
                {
                    cardModel.OwnAddGangCard(tempIncard);
                    cardModel.MoCard();
                }

                #endregion MAHJONG_ACTION_TYPE.AddGang
            }
            else if (newpb.ActionType == MAHJONG_ACTION_TYPE.DarkGang)
            {

                RecordRemainCount();
                RecordRemainCount();
                #region MAHJONG_ACTION_TYPE.DarkGang
                if (newpb.SeatId == mahjong.seatID)
                {

                    //整理牌

                    for (int i = 0; i < cardModel.CardListObj.Count; i++)
                    {
                        cardModel.CardListObj[i].transform.localPosition = new Vector3(cardModel.CardListObj[i].transform.localPosition.x, -307.8f, 0);
                        cardModel.CardListObj[i].GetComponent<CardButton>().TempBool = false;
                    }
                }
                mahjong.tempMahjongActionType = MAHJONG_ACTION_TYPE.OnlyOutCard;
                if (newpb.HandCard == 0)
                {
                    if (mahjong.seatID == mahjong.tempSeatId)
                    {
                        //处理自己的杠牌消息
                        cardModel.OwnDarkGangCard(tempIncard, MAHJONG_ACTION_TYPE.DarkGang, 0);
                    }
                    else
                    {
                        //处理他人的杠牌消息
                        cardModel.OtherDarkGangCard(tempIncard, returnOtherSeatIndex(mahjong.tempSeatId), MAHJONG_ACTION_TYPE.DarkGang);
                    }
                }
                else
                {
                    Debug.Log("cardModel.CardListObj[13].activeSelf:"+ cardModel.CardListObj[13].activeSelf);
                    if (cardModel.CardListObj[13].activeSelf) {
                        
                        cardModel.OwnDarkGangCard(tempIncard, MAHJONG_ACTION_TYPE.DarkGang, CardModel.FourTeenCard);
                        cardModel.MoCard();
                        
                    }
                    else {
                        cardModel.OwnDarkGangCard(tempIncard, MAHJONG_ACTION_TYPE.DarkGang, 0);
                        cardModel.MoCard();
                        
                    }
                    
                }
                #endregion MAHJONG_ACTION_TYPE.DarkGang
            }
            else if (newpb.ActionType == MAHJONG_ACTION_TYPE.LightGang)
            {

                RecordRemainCount();
                

                #region MAHJONG_ACTION_TYPE.LightGang  { "seatId": 2, "nextSid": 2, "actionType": "LIGHT_GANG", "inCard": 4, "handCard": 9 }
                if (newpb.SeatId == mahjong.seatID)
                {

                    //整理牌

                    for (int i = 0; i < cardModel.CardListObj.Count; i++)
                    {
                        cardModel.CardListObj[i].transform.localPosition = new Vector3(cardModel.CardListObj[i].transform.localPosition.x, -307.8f, 0);
                        cardModel.CardListObj[i].GetComponent<CardButton>().TempBool = false;
                    }
                }
                mahjong.tempMahjongActionType = MAHJONG_ACTION_TYPE.OnlyOutCard;

                if (newpb.HandCard == 0)
                {
                    if (mahjong.seatID == mahjong.tempSeatId)
                    {
                        //处理自己的杠牌消息
                        cardModel.OwnGangCard(tempIncard, MAHJONG_ACTION_TYPE.LightGang);
                    }
                    else
                    {
                        //处理他人的杠牌消息
                        cardModel.OtherGangCard(tempIncard, returnOtherSeatIndex(mahjong.tempSeatId), MAHJONG_ACTION_TYPE.LightGang);
                    }
                }
                else
                {
                    cardModel.OwnGangCard(tempIncard, MAHJONG_ACTION_TYPE.LightGang);
                    cardModel.MoCard();
                }
                #endregion MAHJONG_ACTION_TYPE.LightGang
            }
            else if (newpb.ActionType == MAHJONG_ACTION_TYPE.NormalHu)
            {
                cardModel.HuEvent(returnOtherSeatIndex(mahjong.tempSeatId));
                mahjong.HuCard = newpb.InCard;
                mahjong.Huseatid = newpb.SeatId;
                mahjong.whatHu = newpb.ActionType;
            }
            else if (newpb.ActionType == MAHJONG_ACTION_TYPE.GangHu)
            {
                cardModel.HuEvent(returnOtherSeatIndex(mahjong.tempSeatId));
                mahjong.HuCard = newpb.InCard;
                mahjong.Huseatid = newpb.SeatId;
                mahjong.whatHu = newpb.ActionType;
            }
            else if (newpb.ActionType == MAHJONG_ACTION_TYPE.SelfHu)
            {
                cardModel.HuEvent(returnOtherSeatIndex(mahjong.tempSeatId));
                mahjong.HuCard = newpb.InCard;
                mahjong.Huseatid = newpb.SeatId;
                mahjong.whatHu = newpb.ActionType;
            }
            #endregion


            #region 显示东南西北
            //显示东南西北
            if (mahjong.LastNextSid != 0)
            {
                cardModel.DirectionList[returnOtherSeatIndex(mahjong.LastNextSid) - 1].gameObject.SetActive(false);

            }
            //Debug.Log("隐藏ID" + user.LastNextSid);
            if (newpb.NextSid != 0)
            {
                int showDirectionIndex = returnOtherSeatIndex(newpb.NextSid) - 1;
                mahjong.LastNextSid = newpb.NextSid;

                //Debug.Log("显示显示的ID" + showDirectionIndex);
                cardModel.DirectionList[showDirectionIndex].gameObject.SetActive(true);
                cardModel.CountdownCount = 13f;
            }
            #endregion
            //if (newpb.NextSid != 0)
            //{
            //}
        }
        //指导其和、杠、碰、吃
        else if (TempMsgID == MsgId.MahjongSpecialCardNotification)
        {
            Pb.MahjongSpecialCardNotification newpb = Pb.MahjongSpecialCardNotification.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);
            mahjong.havaSpecailCard = true;
            Debug.Log("user.havaSpecailCard receive:" + mahjong.havaSpecailCard);
            mahjong.mahjongSpecialCardItem.Clear();
            mahjong.mahjongSpecialCardItem = newpb.SpecialList.ToList();
            Debug.Log("FirstLoad:"+ FirstLoad);
            if (FirstLoad)
            {
                StartCoroutine(FirstLoadSpecialCard(3.0f));
                FirstLoad = false;
            }
            else {
                GameObject go = LoadModelReturnObj(ChiHuPengGangPath);
                specialCardBtn.Add(go);
                go.GetComponent<SpecialCardModel>().LoadSpecialCard();
            }
        }
        //else if (TempMsgID == MsgId.MahjongSpecialCardBroadcast)  //timeout 有特殊牌
        //{
        //    Pb.MahjongSpecialCardBroadcast newpb = Pb.MahjongSpecialCardBroadcast.Parser.ParseFrom(dataConcent);
        //    Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);
        //}
        //游戏结束处理
        else if (TempMsgID == MsgId.MahjongGameOverBroadcast)
        {

            Pb.MahjongGameOverBroadcast newpb = Pb.MahjongGameOverBroadcast.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);
            //
            mahjong.TempOtherNickName.Clear();
            mahjong.TempOtherAvatarid.Clear();
            for (int i = 1; i <= 4; i++) {
                //mahjong.TempOtherNickName.Add(i,mahjong.OtherNickName[i]);
                //mahjong.TempOtherAvatarid.Add(i, mahjong.OtherAvatarid[i]);
            }

            mahjong.OverRobot.Clear();
            mahjong.OverRoundScore.Clear();
            mahjong.OverRank.Clear();
            mahjong.OverHandCardList.Clear();
            mahjong.OverMahjongSpecialCard.Clear();
            mahjong.roomStatus = newpb.RoomStatus;
            mahjong.OverRobot = newpb.UserList.ToDictionary(t => t.SeatId, t => t.IsRobot);
            mahjong.OverRoundScore = newpb.UserList.ToDictionary(t => t.SeatId, t => t.RoundScore);
            mahjong.TotalScore = newpb.UserList.ToDictionary(t => t.SeatId, t => t.TotalScore);//总战绩
            mahjong.OverRank = newpb.UserList.ToDictionary(t => t.SeatId, t => t.RoundRank);

            mahjong.OverHandCardList = newpb.UserList.ToDictionary(t => t.SeatId, t => t.HandCardList.ToList());
            mahjong.OverMahjongSpecialCard = newpb.UserList.ToDictionary(t => t.SeatId, t => t.SpecialList.ToList());
            //mahjong.OverLackMultiple = newpb.UserList.ToDictionary(t => t.SeatId, t => t.LackCount);
            mahjong.OtherCoinCount.Clear();
            mahjong.OtherScore.Clear();
            mahjong.OtherCoinCount = newpb.UserList.ToDictionary(t => t.SeatId, t => t.BagCoinCount);
            mahjong.OtherScore = newpb.UserList.ToDictionary(t => t.SeatId, t => t.TotalScore);
            //关闭倒计时
            cardModel.CountdownCount = 0f;
            //cardModel.Countdown.gameObject.SetActive(false);
            //游戏结束后存储玩家的游戏结束分数     （不流局）
            List<GameOverUserInfo> gameOverUserInfo = newpb.UserList.ToList();
            List<int> tempRoundScore = new List<int>();
            for (int i = 0; i < mahjong.OverRoundScore.Count; i++)
            {
                tempRoundScore.Add(mahjong.OverRoundScore[i + 1]);
            }
            mahjong.OverRecordList.Add(mahjong.currentRound, tempRoundScore);
            if (user.ControlScene == 1)
            {
                for (int i = 0; i < gameOverUserInfo.Count; i++)
                {

                    int tempIndex =returnOtherSeatIndex(gameOverUserInfo[i].SeatId);
                    user.gameUserInfoList[i].Score += gameOverUserInfo[i].RoundScore;
                    string str = user.ReturnRelCoin(user.gameUserInfoList[i].Score);
                   
                }
            }
            else
            {
                for (int i = 0; i < gameOverUserInfo.Count; i++)
                {

                    int tempIndex = returnOtherSeatIndex(gameOverUserInfo[i].SeatId);
                    user.gameUserInfoList[i].BagCoinCount += gameOverUserInfo[i].RoundScore;
                    string str = user.ReturnRelCoin(user.gameUserInfoList[i].BagCoinCount);

                }
            }
            //加载金币
            for (int i = 0; i < UserList.Count; i++)
            {
                UserList[i].GetComponent<UserStatuModel>().LoadCoin();
                UserList[i].GetComponent<UserStatuModel>().Cover_TuoGuan.SetActive(false);
            }
            GameObject gameOver = GameObject.Find("MahjongCards(Clone)");
            gameOver.GetComponent<GameOverShow>().LoadGameOver(newpb.IsFlow, newpb.IsAllOver, gameOverUserInfo);
            Destroy(gameOver,10f);
            StartCoroutine(LoadGameOver(1.5F));
            FirstLoad = true;
        }

        #endregion

        #region 申请结束游戏处理

        #endregion

        #region 语音 文字 表情
        else if (TempMsgID == MsgId.RoomChatBroadcast)
        {
            Pb.RoomChatBroadcast newpb = Pb.RoomChatBroadcast.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);
            StartCoroutine(TextFaceChat(newpb));
        }
        #endregion



        else if (TempMsgID == MsgId.GameRollNoticeBroadcast)
        {
            Pb.GameRollNoticeBroadcast newpb = Pb.GameRollNoticeBroadcast.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);
        }
        else if (TempMsgID == MsgId.UserCancelRobotBroadcast)
        {
            Pb.UserCancelRobotBroadcast newpb = Pb.UserCancelRobotBroadcast.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);
            int tempseatID=returnOtherSeatIndex(newpb.SeatId)-1;
            UserList[tempseatID].GetComponent<UserStatuModel>().Cover_TuoGuan.SetActive(false);
        }
        else if (TempMsgID == MsgId.NetWorkAbnormalMessage)
        {

            GameObject NetWorkAbnormal = NGUITools.AddChild(GameObject.Find("UI Root").gameObject, (GameObject)(Resources.Load("NetWorkAbnormal")));
        }
    }

    IEnumerator FirstLoadSpecialCard(float waitTime)
    {
        yield return new WaitForSeconds(waitTime);
        GameObject go = LoadModelReturnObj(ChiHuPengGangPath);
        specialCardBtn.Add(go);
        go.GetComponent<SpecialCardModel>().LoadSpecialCard();
    }

    IEnumerator LoadGameOver(float waitTime)
    {
        yield return new WaitForSeconds(waitTime);
        GameObject.Find("MahjongCards(Clone)").GetComponent<GameOverShow>().GameOverInfo.SetActive(true);

    }
    IEnumerator GoHall(float waitTime)
    {
        yield return new WaitForSeconds(waitTime);
        Application.LoadLevel("Hall");
        ClearRecord();
    }
    IEnumerator GoHallBot(float waitTime)
    {
        yield return new WaitForSeconds(waitTime);
        //Application.LoadLevel("Hall");
    }

    IEnumerator CloseGame(float waitTime)
    {
        yield return new WaitForSeconds(waitTime);
        Application.LoadLevel("Hall");
        ClearRecord();
    }

    //对于FixUI（返回的）处理
    void BackBtnEvent()
    {
        //发送离开消息
        if (mahjong.CteatrUid == mahjong.uid)
        {
            Debug.Log("解散房间");
            RoomDismissRequest lg = new Pb.RoomDismissRequest { RoomId = mahjong.roomID };
            SocketManager socketManager = SocketManager.Instance;
            socketManager.SendProtobuf1(lg, MsgId.RoomDismissRequest);

        }
        else
        {
            Debug.Log("离开房间");
            RoomLeaveRequest lg = new Pb.RoomLeaveRequest { RoomId = mahjong.roomID };
            SocketManager socketManager = SocketManager.Instance;
            socketManager.SendProtobuf1(lg, MsgId.RoomLeaveRequest);

        }
    }

    #region LoadUser() 加载玩家
   
    //加载玩家
    void LoadUser()
    {

        mahjong.LoadIndex(mahjong.seatID);
        for (int i = 0; i < user.gameUserInfoList.Count; i++) {
            int addseatID = user.gameUserInfoList[i].SeatId;
            int addIndex = returnOtherSeatIndex(addseatID) - 1;
            UserList[addIndex].SetActive(true);
            Debug.Log("addseatID"+addseatID);
            UserList[addIndex].GetComponent<UserStatuModel>().LoadInfo(addseatID);
            string str = "";
            if (user.gameUserInfoList[i].SeatStatus == SEAT_STATUS.Ready)
            {
                str = "准备中";
                UserList[addIndex].GetComponent<UserStatuModel>().readyStatu();
            }
            else if (user.gameUserInfoList[i].SeatStatus == SEAT_STATUS.Sit)
            {
                str = "等待中";
            }
            else if (user.gameUserInfoList[i].SeatStatus == SEAT_STATUS.Offline)
            {
                str = "离线";
                UserList[addIndex].GetComponent<UserStatuModel>().LeaveStatu();
            }
        }

        
    }
    private int returnOtherSeatIndex(int seatid)
    {
        
        int tempReture = mahjong.returnOtherSeatIndex(seatid);
        
        return tempReture;
    }
    #endregion LoadUser() 加载玩家

    #region 向服务器发送消息
    //发送离开房间消息

    void sendLeaveRoom()
    {
        RoomLeaveRequest RR = new Pb.RoomLeaveRequest { RoomId = mahjong.roomID };
        SocketManager socketManager = SocketManager.Instance;
        socketManager.SendProtobuf1(RR, MsgId.RoomLeaveRequest);
    }

    //发送解散游戏消息
    //void sendCloseGame()
    //{
    //    RoomCloseRequest RR = new Pb.RoomCloseRequest { RoomId = mahjong.roomID };
    //    SocketManager socketManager = SocketManager.Instance;
    //    socketManager.SendProtobuf1(RR, MsgId.RoomCloseRequest);
    //}

    #endregion 向服务器发送消息

    #region 筛子动画 


    private DiceModel diceModel;
    private string dicePath = "GameMahjong/NextGameP/Effect/Dice";
    IEnumerator DoDiceAnimation(List<int> readyCards)
    {
        GameObject go = LoadModelReturnObj(dicePath);
        diceModel = go.GetComponent<DiceModel>();
        diceModel.diceAniObj.SetActive(true);
        yield return StartCoroutine(DoPauseDiceAni(1.5F, readyCards));

    }
    IEnumerator DoPauseDiceAni(float waitTime, List<int> readyCards)
    {
        yield return new WaitForSeconds(waitTime);
        diceModel.diceAniObj.SetActive(false);
        diceModel.dice1.gameObject.SetActive(true);
        diceModel.dice1.spriteName = mahjong.diceOne.ToString();

        diceModel.dice2.gameObject.SetActive(true);
        diceModel.dice2.spriteName = mahjong.diceTwo.ToString();
        yield return StartCoroutine(DoLoadCard(1.5F, readyCards));
    }
    IEnumerator DoLoadCard(float waitTime, List<int> readyCards)
    {
        yield return new WaitForSeconds(waitTime);
        Debug.Log("加载card");
        //加载牌
        Destroy(diceModel.gameObject);
        LoadModel(CardsPath);
        cardModel = GameObject.Find("MahjongCards(Clone)").GetComponent<CardModel>();
        mahjong.tempMahjongActionType = MAHJONG_ACTION_TYPE.Normal;
        if (readyCards.Count > 0)
        {
            cardModel.tingCard.SetActive(true);
            cardModel.tingBgList[0].SetActive(true);
            Debug.Log("ReadyCards" + readyCards.Count);
            for (int i = 0; i < cardModel.tingCardList.Count; i++)
            {
                cardModel.tingCardList[i].gameObject.SetActive(false);
            }


            cardModel.tingBgList[0].GetComponent<UISprite>().width = 43 + 26 * (readyCards.Count - 1);
            for (int i = 0; i < readyCards.Count; i++)
            {
                cardModel.tingCardList[i].gameObject.SetActive(true);
                cardModel.tingCardList[i].spriteName = readyCards[i].ToString();
            }
        }
        else
        {
            cardModel.tingCard.SetActive(false);
            for (int i = 0; i < cardModel.tingCardList.Count; i++)
            {

                cardModel.tingCardList[i].gameObject.SetActive(false);
            }
        }
        

    }
    #endregion


    /// <summary>
    /// 文字表情聊天   
    /// </summary>
    /// <param name="newpb"></param>
    /// <returns></returns>  
    IEnumerator TextFaceChat(RoomChatBroadcast newpb)
    {
        int tempSeatID = 0;
        foreach (KeyValuePair<int, int> kvp in mahjong.Otheruid)
        {
            if (newpb.FromUid == kvp.Value)
            {
                tempSeatID = kvp.Key;
                break;
            }
        }
        int tempIndex = 0;
        tempIndex = returnOtherSeatIndex(tempSeatID)-1;
        if (newpb.MsgType == RoomChatType.TextId)
        {
            UserList[tempIndex].GetComponent<UserStatuModel>().textObj.SetActive(true);
            UserList[tempIndex].GetComponent<UserStatuModel>().text.text = mahjongText[int.Parse(newpb.Content) - 1];
            if (mahjong.OtherSex[tempSeatID] == SEX.Male)
            {
                SoundModel.PlaySound(TextaudioClipListMan[int.Parse(newpb.Content) - 1]);
            }
            else
            {
                SoundModel.PlaySound(TextaudioClipListWomen[int.Parse(newpb.Content) - 1]);
            }
        }
        else if (newpb.MsgType == RoomChatType.MovieClip)
        {
            UserList[tempIndex].GetComponent<UserStatuModel>().face.gameObject.SetActive(true);
            UserList[tempIndex].GetComponent<UserStatuModel>().face.spriteName = newpb.Content;
        }
        yield return new WaitForSeconds(2.5f);
        UserList[tempIndex].GetComponent<UserStatuModel>().textObj.SetActive(false);
        UserList[tempIndex].GetComponent<UserStatuModel>().face.gameObject.SetActive(false);
    }

    void roomInfoShow() {

        if (user.ControlScene == 1)
        {
            roomInfoLable.text = "房间号：" + mahjong.roomID + "    " + "第" + mahjong.currentRound + "局";
        }
        else
        {
            roomInfoLable.text = "金币场            " + "第" + mahjong.currentRound + "局";
        }
    }
    void RecordRemainCount()
    {
        mahjong.remainCount -= 1;
        cardModel.remainCount.text = mahjong.remainCount.ToString();
    }
  
    void LoadModel(string TempPath)
    {
        UnityEngine. Object Obj = Resources.Load(TempPath, typeof(GameObject));
        GameObject go = Instantiate(Obj, Vector3.zero, transform.rotation, this.transform) as GameObject;
    }
    GameObject LoadModelReturnObj(string TempPath)
    {
        UnityEngine. Object Obj = Resources.Load(TempPath, typeof(GameObject));
        GameObject go = Instantiate(Obj, Vector3.zero, transform.rotation, this.transform) as GameObject;
        return go;
    }

    private void OnDestroy()
    {
        ClearOtherUserInfo();
    }

    private void ClearRecord() {
        user.gameUserInfoList.Clear();
        mahjong.OverRoundScore.Clear();
        mahjong.TotalScore.Clear();
        mahjong.OverRank.Clear();
        mahjong.OverHandCardList.Clear();
        mahjong.OtherCoinCount.Clear();
        mahjong.currentRound = 0;
        mahjong.OverRecordList.Clear();
        mahjong.OverRobot.Clear();
        mahjong.mahjongSpecialCardItem.Clear();
        mahjong.OtherScore.Clear();
        ClearOtherUserInfo();
    }


    //清除其他玩家信息
    void ClearOtherUserInfo()
    {
        mahjong.OverRobot.Clear();
        mahjong.Otheruid.Clear();
        mahjong.OtherSeatId.Clear();
        mahjong.OtherSeatStatus.Clear();
        mahjong.OtherSex.Clear();
        mahjong.OtherIP.Clear();
        mahjong.OtherCoinCount.Clear();
        mahjong.OtherNickName.Clear();
        mahjong.OtherAvatarid.Clear();
        mahjong.bankerSid = 0;
    }


    /// <summary>
    /// 加载微信头像
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    IEnumerator GetWWWFACE(UITexture texture, string url)
    {

        if (url != "")
        {
            WWW www = new WWW(url);


            yield return www;
            if (www.isDone)
            {
                if (www.error == null) { }
                texture.mainTexture = www.texture;
                texture.gameObject.SetActive(true);
            }
            else
            {

            }
            www.Dispose();
        }

    }
   
}